<template>
  <div style="padding: 10px; width: 100%">
    <div style="margin: 10px 0">
      <el-input
        placeholder="请输入年级名称"
        v-model="search"
        style="width: 200px"
      >
        <template #append>
          <el-button icon="el-icon-search" @click="load"></el-button>
        </template>
      </el-input>

      <el-button type="primary" style="margin-left: 20px" @click="add"
        >添加</el-button
      >
    </div>
    <el-table :data="tableData" border stripe style="width: 80%">
      <el-table-column prop="id" label="ID" align="center" />
      <el-table-column prop="name" label="年级名称" align="center" />
      <el-table-column label="年级课程" align="center">
        <template #default="scope">
          <div class="name-wrapper">
            <el-tag
              size="mini"
              style="margin-left: 10px"
              v-for="item in scope.row.courseList"
              :key="item.id"
              >{{ item.name }}</el-tag
            >
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            @click="handleEdit(index, scope.row)"
            size="mini"
            type="primary"
            plain
            >编辑</el-button
          >
          <el-button
            @click="handleDelete(scope.row.id)"
            size="mini"
            type="danger"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="添加年级信息"
      v-model="dialogVisible"
      width="30%"
      :before-close="dialogClose"
    >
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="年级编号" prop="id">
          <el-input v-model.number="form.id" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="年级名称" prop="name">
          <el-input v-model="form.name" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="课程">
          <el-select v-model="form.courseList" multiple placeholder="请选择">
            <el-option
              v-for="item in courseinfo"
              :key="item.value"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addGrade">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog
      title="修改年级信息"
      v-model="updateVisible"
      width="30%"
      :before-close="dialogClose"
    >
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="年级编号" prop="id">
          <el-input
            v-model.number="form.id"
            style="width: 200px"
            disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="年级名称" prop="name">
          <el-input v-model="form.name" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="课程" prop="name">
          <el-select
            v-model="form.courseList"
            multiple
            placeholder="请选择"
            @change="$forceUpdate()"
          >
            <el-option
              v-for="item in courseinfo"
              :key="item.name"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="updateVisible = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "../../utils/request";

export default {
  name: "Grade",
  data() {
    return {
      rules: {
        id: [
          { required: true, message: "年级编号不能为空", trigger: "blur" },
          { type: "number", message: "年级编号必须为数值类型" },
          
        ],
        name: [{ required: true, message: "年级不能为空", trigger: "blur" }],
      },

      search: "",
      // 下拉列表
      courseinfo: [],
      courseList: {},
      value: "",
      inputVisible: false,
      courseId: "",
      gradeId: "",
      gcId: "",
      tableData: [],
      form: {},
      dialogVisible: false,
      updateVisible: false,
    };
  },
  created() {
    this.findGrade();
  },
  methods: {
    update() {
      console.log(this.form);
      request.put("/grade/update", this.form).then((res) => {
        if (res.code === "0") {
          this.$message({ type: "success", message: "更新成功" });
          console.log(res);
          this.updateVisible = false;
          this.load();
        } else {
          this.$message({ type: "error", message: "更新失败" });
        }
      });
    },
   
    // 查询
    load() {
      this.findGrade();
    },
    // 打开添加课程框
    add() {
      this.dialogVisible = true;
      this.findCourse();
      this.form = {};
    },
    // 打开编辑框
    handleEdit(index, row) {
      this.findCourse();
      this.updateVisible = true;
      this.form = Object.assign({}, row);
      console.log("form");
      console.log(this.form);
      this.form.courseList = row.courseList.map((v) => v.id);
    },
    // 添加年级信息
    addGrade() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          console.log(this.form);
          request.post("/grade/add", this.form).then((res) => {
            if (res.code === "0") {
              this.$message({ type: "success", message: "添加成功" });
              this.dialogVisible = false;
              this.load();
            } else {
              this.$message({ type: "error", message: res.msg });
            }
          });
        }
      });
    },
    // 查询年级信息
    findGrade() {
      request
        .get("/grade/list", {
          params: {
            id: this.search,
          },
        })
        .then((res) => {
          this.tableData = res.data;
          console.log(this.tableData);
        });
    },
    // 查询课程信息
    findCourse() {
      request.get("/course/all", {}).then((res) => {
        console.log(res);
        this.courseinfo = res;
      });
    },
    //删除学生
    handleDelete(id) {
      // alert(id)
      console.log(id);
      this.$confirm("是否删除该年级信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          request.delete("/grade/delete/" + id).then((res) => {
            if (res.code === "0") {
              this.$message({
                type: "success",
                message: "删除成功",
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg(),
              });
            }
            this.load();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

